using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    [Header("移动相关")]
    public float flyForce = 10f;
    public float glideGravityScale = 0.5f;
    public float fallGravityScale = 2.5f;
    public float normalGravityScale = 1f;

    [Header("耐力相关")]
    public float maxStamina = 100f;
    public float currentStamina;
    public float staminaDrainRate = 30f;
    public float staminaRecoverRate = 20f;

    private Rigidbody2D rb;
    private bool isGrounded = false;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        currentStamina = maxStamina;
    }

    void Update()
    {
        HandleFlightInput();
        HandleStamina();
    }

    void HandleFlightInput()
    {
        if (Input.GetKey(KeyCode.W) && currentStamina > 0)
        {
            // 飞行：向上推力 + 消耗耐力
            rb.velocity = new Vector2(rb.velocity.x, flyForce);
            currentStamina -= staminaDrainRate * Time.deltaTime;
            rb.gravityScale = 0f;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            // 快速下落
            rb.gravityScale = fallGravityScale;
        }
        else
        {
            // 滑翔状态
            rb.gravityScale = glideGravityScale;
        }
    }

    void HandleStamina()
    {
        // 地面上可以恢复耐力
        if (isGrounded && currentStamina < maxStamina)
        {
            currentStamina += staminaRecoverRate * Time.deltaTime;
        }

        // 保证耐力不超出范围
        currentStamina = Mathf.Clamp(currentStamina, 0f, maxStamina);
    }

    // 地面检测（用碰撞体）
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Ground"))
            isGrounded = true;
    }

    void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Ground"))
            isGrounded = false;
    }
}
